package com.tool.btn;

import com.tool.tab.Main;
import com.tool.tab.TabJFrame;
import com.tool.tcpimg.UIUtils;
import org.come.Jpanel.CustomTitleBarUI;
import org.come.control.ExitDialogLogin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

public class TitlelBtn extends MoBanBtn {
    private int caozuo;
    private Boolean b = false;
    private Boolean gl = false;
    private TitlelBtn delete;
    private int changForm = 1;
    private JFrame jFrame;
    private int paneIndex = 0;


    public TitlelBtn(final String iconpath, final int type, final String text, int caozuo) {
        super(iconpath, type);
        this.setText(text);
        this.setFont(UIUtils.TEXT_FONT1);
        this.setForeground(Color.orange);
        this.setVerticalTextPosition(0);
        this.setHorizontalTextPosition(0);
        this.caozuo = caozuo;
    }


    public TitlelBtn(final String iconpath, final int type, final String text, int caozuo, JFrame jFrame) {
        super(iconpath, type);
        this.setText(text);
        this.setFont(UIUtils.TEXT_FONT1);
        this.setForeground(Color.orange);
        this.setVerticalTextPosition(0);
        this.setHorizontalTextPosition(0);
        this.jFrame = jFrame;
        this.caozuo = caozuo;
    }


    public TitlelBtn(final String iconpath, final int type, final String text, int caozuo, Boolean b, TitlelBtn t) {
        super(iconpath, type);
        this.setText(text);
        this.setFont(UIUtils.TEXT_FONT1);
        this.setForeground(Color.orange);
        this.setVerticalTextPosition(0);
        this.setHorizontalTextPosition(0);
        this.caozuo = caozuo;
        this.b = b;
    }


    @Override
    public void chooseyes() {
    }

    @Override
    public void chooseno() {
    }

    @Override
    public void nochoose(final MouseEvent e) {
        if (caozuo == 99) {

            if(Main.tabJFrame.customTitleBarUI.getTitlelBtns().size() > 4){
                //  显示消息对话框
                JOptionPane.showMessageDialog(Main.tabJFrame,
                        "最多5个窗口,如需更多请再次运行客户端。",
                        "提示信息",
                        JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            TabJFrame jFrame = (TabJFrame) this.jFrame;
//            jFrame.createContent(-1);
            // 添加游戏到窗口
            int i = jFrame.addGame();
            // 设置窗口标题栏的索引
            Main.tabJFrame.customTitleBarUI.setIndex(i);
            // 改变窗口标题栏的选中状态
            Main.tabJFrame.customTitleBarUI.changeSelect(i);
            // 刷新角色
            TitlelBtn titlelBtn = Main.tabJFrame.customTitleBarUI.refreshRoles();
            // 设置窗口选项卡的索引
            Main.tabJFrame.jTabbedPane.setSelectedIndex(i);
            // 将标题按钮设置为未选中状态
            titlelBtn.nochoose(null);
            // 设置窗口大小
            Main.tabJFrame.setSize(1026, 768);
            // 初始化窗口标题栏
            Main.tabJFrame.customTitleBarUI.initJD();
            // 设置窗口开始状态
            Main.tabJFrame.start = true;
            // 显示聊天框
            chatBoxShowAndHide();
            // 显示游戏
            Main.tabJFrame.showGame(i);

//            Main.tabJFrame.customTitleBarUI.getC1().nochoose(null);

//            ButtonTabComponent tabComponentAt = (ButtonTabComponent)jFrame.jTabbedPane.getTabComponentAt(jFrame.jTabbedPane.getTabCount() - 1);
//            MouseListener[] mouseListeners1 = tabComponentAt.getMouseListeners();
//
////            MouseListener[] mouseListeners = tabComponentAt.getMouseListeners();
        } else if (caozuo == 96) {
            Main.tabJFrame.setExtendedState(JFrame.ICONIFIED);
//            Main.tabJFrame.games.forEach((k, v) -> {
//                Process process = Main.tabJFrame.games.get(k);
////                if (k == caozuo) {
////                    try {
////                        process.getOutputStream().write(("chatShow" + "\n").getBytes());
////                        process.getOutputStream().flush();
////                    } catch (IOException ioException) {
////                        ioException.printStackTrace();
////                    }
////                } else {
//                    try {
//                        process.getOutputStream().write(("chatHide" + "\n").getBytes());
//                        process.getOutputStream().flush();
//                    } catch (IOException ioException) {
//                        ioException.printStackTrace();
//                    }
////                }
//            });
//            if (TestMain.chatBoxJframe != null)
//                TestMain.chatBoxJframe.setVisible(false);
        } else if (caozuo >= 100 && caozuo <= 9999) {
            int i = caozuo - 100;
            Process process = Main.tabJFrame.getGames().get(i);
            if (process != null)
                process.destroy();
            for (Iterator<Map.Entry<Integer, Process>> iterator = Main.tabJFrame.games.entrySet().iterator();
                 iterator.hasNext();
            ) {
                Map.Entry<Integer, Process> entry = iterator.next();
                Main.hwnds.put(entry.getKey(), null);
                process = Main.tabJFrame.games.get(entry.getKey());
                if (!process.isAlive()) {
                    Main.tabJFrame.removeGame(entry.getKey());
//                    Main.tabJFrame.setIndex(TabJFrame.index - 1);
                    iterator.remove();
                }
            }

            Main.tabJFrame.roles.remove(i);
//            Main.tabJFrame.customTitleBarUI.setIndex(i == 0 ? 0 : i - 1);
//            Main.tabJFrame.customTitleBarUI.changeSelect();
            Main.tabJFrame.removeGame(i);

            TitlelBtn titlelBtn = Main.tabJFrame.customTitleBarUI.refreshRoles();
            if (titlelBtn == null) {
//                Main.tabJFrame.customTitleBarUI.getC1().nochoose(null);
//                titlelBtn = Main.tabJFrame.customTitleBarUI.refreshRoles();
            } else
                titlelBtn.nochoose(null);
//            Main.tabJFrame.jTabbedPane.setSelectedIndex(i == 0 ? 0 : i - 1);
//            Main.tabJFrame.showGame(i == 0 ? 0 : i - 1);

        } else if (caozuo == 97) {
//            System.exit(1);
            new ExitDialogLogin(Main.tabJFrame).setVisible(true);
        } else if (b) {
            CustomTitleBarUI.isShow = false;
//            Main.tabJFrame.getJdbg().setVisible(false);
            Main.tabJFrame.customTitleBarUI.refreshRoles();
            Main.tabJFrame.customTitleBarUI.setIndex(caozuo);
            Main.tabJFrame.customTitleBarUI.changeSelect(caozuo);
            Main.tabJFrame.jTabbedPane.setSelectedIndex(caozuo);
            Main.tabJFrame.showGame(caozuo);
            chatBoxShowAndHide();
            CustomTitleBarUI.isShow = true;
//            Main.tabJFrame.getJdbg().setVisible(true);
        }
//        else if (this.b) {
//
//
//            if (TestMain.gameJframe.customTitleBarUI.getIndex() == caozuo && SendMessageUntil.getGameClient().getLoginResult() != null)
//                return;
////            Integer selectIndex = SendMessageUntil.getSelectIndex(caozuo + "");
//            TestMain.gameJframe.customTitleBarUI.setIndex(caozuo);
//            TestMain.gameJframe.customTitleBarUI.changeSelect();
//            GameClient g = SendMessageUntil.gameClients.get(caozuo);
//            SendMessageUntil.channel = g.getChannel();
//            if (g != null && g.getLoginResult() == null) {
//                if (TestMain.gameJframe != null) {
//                    if (GameJpanel.gameJpanel != null)
//                        GameJpanel.gameJpanel.setVisible(false);
////                        TestMain.gameJframe.remove(GameJpanel.gameJpanel);
//                    TestMain.gameJframe.loginJpanel.setVisible(true);
//                    TestMain.gameJframe.loginJpanel.getSystemPrompt().clear();
////                    TestMain.gameJframe.add(TestMain.gameJframe.loginJpanel);
//                    TestMain.gameJframe.setSize(1020 + 8, 710 + 30 + GameJframe.titleHeight + 30);
//                    if (SendMessageUntil.getGameClient().getChangeFormId() == 2) {
//                        TestMain.gameJframe.loginJpanel.setRoleArr(g.getRoleTableList());
//                    }
//                    TestMain.gameJframe.loginJpanel.framechange(SendMessageUntil.getGameClient().getChangeFormId(), null, TestMain.gameJframe.loginJpanel.getLoginInfo());
//
//                    if (TestMain.chatBoxJframe != null) {
//                        TestMain.chatBoxJframe.setVisible(false);
//                    }
//                }
//            } else {
////                TestMain.gameJframe.remove(TestMain.gameJframe.loginJpanel);
//                TestMain.gameJframe.loginJpanel.setVisible(false);
//                if (GameJpanel.gameJpanel != null) {
//                    if (g != null) {
//                        GoodsListFromServerUntil.isSendPackRecord();
//                    }
//                    ImageMixDeal.initAll();
//                    GameClient gameClient = SendMessageUntil.getGameClient();
//                    String serverMes = Agreement.getAgreement().switchEnterGameAgreement(gameClient.DXCL);
//                    SendMessageUntil.toServer(serverMes);
//                }
//                GameJpanel.gameJpanel.setVisible(true);
//
////                    TestMain.gameJframe.add(GameJpanel.getGameJpanel());
//                if (TestMain.chatBoxJframe != null) {
//                    TestMain.chatBoxJframe.setVisible(true);
//                }
//
//            }
//            Main.frame.customTitleBarUI.setTitle();

//        }
    }


    private void chatBoxShowAndHide() {
        Main.tabJFrame.games.forEach((k, v) -> {
            Process process = Main.tabJFrame.games.get(k);
            if (k == caozuo) {
                try {
                    process.getOutputStream().write(("chatShow" + "\n").getBytes());
                    process.getOutputStream().flush();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            } else {
                try {
                    process.getOutputStream().write(("chatHide" + "\n").getBytes());
                    process.getOutputStream().flush();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        if (btn == -1) {
            return;
        }
        if (btn != 0 && zhen != 2) {
            btnchange(1);
        }
        type = 1;
    }

    @Override
    public void mouseExited(MouseEvent e) {
        if (!gl) {
            // TODO Auto-generated method stub
            if (btn == -1) {
                return;
            }
            if ((btn != 0 || btn != -1) && zhen != 2) {
                btnchange(0);
            }
            type = 0;
        }
    }

    public Boolean getGl() {
        return gl;
    }

    public void setGl(Boolean gl) {
        this.gl = gl;
    }

    public TitlelBtn getDelete() {
        return delete;
    }

    public void setDelete(TitlelBtn delete) {
        this.delete = delete;
    }

    public int getChangForm() {
        return changForm;
    }

    public void setChangForm(int changForm) {
        this.changForm = changForm;
    }

    public int getCaozuo() {
        return caozuo;
    }

    public void setCaozuo(int caozuo) {
        this.caozuo = caozuo;
    }
}
